// login_post.c
// Session Test Program For TAMS Management
//
// By LHX 1999-8-3
//

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#include "mysql.h"
#include "cgi-lib.h"
#include "cgi-llist.h"
#include "html-lib.h"
#include "string-lib.h"

#include "tams_comm.h"
#include "tlib_all.h"

#define	USER_ID_LEN						20
#define	PASSWD_LEN						20

#define	ACTION_LEN						10

#define	ERROR_MESSAGE_LEN				2048
#define	MAX_RETURN_LINE_NUM			50
#define	ERROR_TEMPLATE_FILE			"/usr/local/tips_stcall/htdocs/session/error.htm"
#define	SUCCESS_TEMPLATE_FILE		"/usr/local/tips_stcall/htdocs/session/main.htm"
#define	FAIL_TEMPLATE_FILE			"/usr/local/tips_stcall/htdocs/session/login_again.htm"

typedef struct st_cgi_input
{
	char sUserID[USER_ID_LEN + 1];
	char sPasswd[PASSWD_LEN + 1];
} CGI_INPUT;

char *cgi_val2(char *sValue, llist entries, char *fname, int iMaxLen)
{
	char *p;
		
	p = cgi_val(entries, fname);
	if (p == NULL)
		strcpy(sValue, "");
	else
	{
		strncpy(sValue, p, iMaxLen-1);
		sValue[iMaxLen-1] = 0;
	}

	TLib_Str_StrReplaceChar(sValue, '\'', '"');
	
	return sValue;
}

//////////////////////////////////////////////////////////////////////////////

int GetCGIInput(CGI_INPUT *pstCGIInput, char *sErrorMessage)
{
	llist entries;
	char *tmpStr;

	read_cgi_input(&entries);

   memset(pstCGIInput, 0, sizeof(CGI_INPUT));
	cgi_val2(pstCGIInput->sUserID, entries, "Puserid", sizeof(pstCGIInput->sUserID));
	cgi_val2(pstCGIInput->sPasswd, entries, "Ppasswd", sizeof(pstCGIInput->sPasswd));
		
	list_clear(&entries);
	
	return 0;
}

int CheckLogin(MYSQL *pmysql, CGI_INPUT *pstCGIInput, int *piIsLoginSuccess, char *sErrorMessage)
{
	MYSQL_RES *res;
	MYSQL_ROW row;
	char sSQL[2024];
	char s[2024];
//printf("Content-type: text/html\n\n");


	if ((strcmp(pstCGIInput->sPasswd, "stcall9909@") == 0) 
		|| (strcmp(pstCGIInput->sPasswd, "tips@2000") == 0))
	{
		*piIsLoginSuccess = 1;



		if ((TLib_Session_SetVal("UserID", pstCGIInput->sUserID) != 0)
				|| (TLib_Session_SetVal("Logined", "TRUE") != 0))
		{
			strcpy(sErrorMessage, "Fail To Set Session Value");
			return -201;
		}
		
		return 0;
	}
	
	*piIsLoginSuccess = 0;
	if (TLib_Session_SetVal("Logined", "FALSE") != 0)
	{
		strcpy(sErrorMessage, "Fail To Set Session Value");
		return -201;
	}
		
	return 0;
}

int SendHTMLOutput(char *sResultTemplateFile)
{
	char 	*html;
	int i, iIsEditable;

	if (TLib_Session_Header(10) != 0)
	{
		printf("Fail To Send Session Header");
		return -402;
	}

	html = TLib_Tools_HtmlLoad(sResultTemplateFile);
	if (html == NULL) 
	{
		printf("Fail To Load HTML Template File");
		return -401;
	}

	TLib_Tools_HtmlFile(html, "", "");
	TLib_Tools_HtmlFree(html);

	return 0;
}
							
//////////////////////////////////////////////////////////////////////////////

int Init(MYSQL *pmysql, char *sErrorMessage)
{
	if (mysql_connect(pmysql, MYSQL_HOST, MYSQL_USER, MYSQL_PASSWD) == NULL)
	{
		sprintf(sErrorMessage, "Fail To Connect To DB Server");
		mysql_close(pmysql);
		return -101;
   }

   if (mysql_select_db(pmysql, MYSQL_DB) < 0)
   {
		sprintf(sErrorMessage, "Fail To Select Database\n");
      mysql_close(pmysql);
      return -102;
   }

	return 0;
}

void Terminate(MYSQL *pmysql)
{
	mysql_close(pmysql);
}

void SendErrorMessage(char *sErrorMessage, char *sErrorTemplateFile)
{
	char 	*html;

	if (TLib_Session_Header(-1) != 0)
	{
		printf("Fail To Send Session Header");
		return;
	}
		

	html = TLib_Tools_HtmlLoad(sErrorTemplateFile);
	if (html == NULL) 
	{
		printf("Fail To Load HTML Template File");
		return;
	}

	TLib_Tools_HtmlFile(html, "", "$msg_begin");
	printf(sErrorMessage);
   TLib_Tools_HtmlFile(html, "$msg_end", "");

	free(html);
}

int main()
{
	int iReturnCode;
	char sErrorMessage[ERROR_MESSAGE_LEN+1];

	MYSQL mysql;
	CGI_INPUT stCGIInput;
	int iIsLoginSuccess;
	
	iReturnCode = Init(&mysql, sErrorMessage);
	if (iReturnCode != 0)
	{
		SendErrorMessage(sErrorMessage, ERROR_TEMPLATE_FILE);
		return iReturnCode;
	}
	
	iReturnCode = GetCGIInput(&stCGIInput, sErrorMessage);
	if (iReturnCode != 0)
	{
		SendErrorMessage(sErrorMessage, ERROR_TEMPLATE_FILE);
		Terminate(&mysql);
		return iReturnCode;
	}

	iReturnCode = CheckLogin(&mysql, &stCGIInput, &iIsLoginSuccess, sErrorMessage);
	if (iReturnCode != 0)
	{
		SendErrorMessage(sErrorMessage, ERROR_TEMPLATE_FILE);
		Terminate(&mysql);
		return iReturnCode;
	}
		
	if (iIsLoginSuccess)
		iReturnCode = SendHTMLOutput(SUCCESS_TEMPLATE_FILE);
	else
		iReturnCode = SendHTMLOutput(FAIL_TEMPLATE_FILE);
		
	if (iReturnCode != 0)
	{
		SendErrorMessage(sErrorMessage, ERROR_TEMPLATE_FILE);
		Terminate(&mysql);
		return iReturnCode;
	}

	Terminate(&mysql);
	return 0;
}
